# encoding: utf-8

import time
import uuid

import scrapy
from scrapy import Request, FormRequest

# from App_Spd.items import AppSpdItem
# from App_Spd.utils import analysis, get_urlid, get_pubtime

from app_spider.items import AppSpdItem
from app_spider.utils import analysis, get_urlid, get_pubtime

headers = {
    'Host': 'jbapp.cnnb.com.cn',
    'Accept': '*/*',
    'version': '14.6',
    'appversion': '1.1.0',
    'Accept-Language': 'zh-Hans;q=1',
    'Accept-Encoding': 'gzip,deflate',
    'system': 'IOS',
    'deviceid': str(uuid.uuid1()),
    'User-Agent': 'xin jiang bei/1.1.0 (iPhone; iOS 14.6; Scale/3.00)',
    'appbuild': '202109240',
    'model': 'iPhone13,4'

}


class XinjiangbeiAppSpider(scrapy.Spider):
    websiteId = '2141422584'
    pubSource = '新江北'
    name = 'xinjiangbei_app'
    custom_settings = {'DOWNLOAD_DELAY': 0}
    channel_api = 'http://jbapp.cnnb.com.cn/yongpai-news/api/news/list?channelId={}&currentPage=1&timestamp={}'
    detail_api = 'https://jbapp.cnnb.com.cn/yongpai-news/api/news/detail?newsId={}'

    def start_requests(self):
        '''
        0 头条
        93 政情
        110 防疫
        62 媒体看江北
        97 聚焦
        98 专题
        '''
        channelIdList = [0, 93, 110, 62, 97, 98]
        for channel in channelIdList:
            url = self.channel_api.format(channel, int(time.time()))
            yield Request(url=url, callback=self.parse, headers=headers)

    def parse(self, response):
        for content in response.json()['data']['content']:
            newsid = content.get('newsId')
            if newsid:
                yield Request(url=self.detail_api.format(newsid), headers=headers, callback=self.parse_detail)

    def parse_detail(self, response):
        resp = response.json()

        item = AppSpdItem()
        item['title'] = resp.get('data').get('title')
        item['author'] = resp.get('data').get('author')
        item['url'] = response.url
        item['pubtime'] = resp.get('data').get('sourcetime')
        item['content'] = analysis(resp.get('data').get('body'))
        item['websiteId'] = 2141422584  # 提供app对应的id
        item['pubSource'] = "新江北"  # 提供app对应名称
        item['urlid'] = get_urlid(item['url'])
        yield item


